package top.maof.book.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.maof.book.model.Role;
import top.maof.book.model.Score;
import top.maof.book.result.ApiResponse;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.result.Result;
import top.maof.book.service.ScoreService;
import top.maof.book.utils.JWTUtils;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/score")
@ResponseBody
public class ScoreController {
    @Autowired
    ScoreService scoreService;

    @ApiOperation("新建")
    @PostMapping("/create")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse create(@RequestBody Score score) {
        return ApiResponse.success(scoreService.save(score));
    }

    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse deleteById(@PathVariable Integer id) {
        scoreService.removeById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse deleteByIdInBatch(@RequestParam String ids) {
        String[] idArray = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Integer.valueOf(id));
        }
        scoreService.removeBatchByIds(idList);
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse updateById(@RequestBody Score score) {
        scoreService.updateById(score);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse<PageData<Score>> findPage(@RequestBody PageWrap<Score> pageWrap) {
        return ApiResponse.success(scoreService.findPage(pageWrap));
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse findById(@PathVariable Integer id) {
        return ApiResponse.success(scoreService.getById(id));
    }

    //获取书的评分
    @ResponseBody
    @RequestMapping("book")
    public Result bookScore(Integer bookId) throws Exception {
        double v = scoreService.bookScore(bookId);
        return Result.success(v);
    }

    // 获取用户对书的评分
    @ResponseBody
    @RequestMapping("get")
    public Result get(@RequestHeader("token") String token, Integer bookId) throws Exception {
        Integer userId = JWTUtils.getUserId(token);
        Score score = scoreService.findScoreByUserAndBook(userId, bookId);
        return Result.success(score);
    }

    // 用户开始评分
    @ResponseBody
    @RequestMapping("evaluate")
    public Result add(Double score, Integer bookId, @RequestHeader("token") String token) throws Exception {
        Score s = new Score();
        s.setScore(score);
        s.setBookId(bookId);
        s.setUserId(JWTUtils.getUserId(token));
        s.setTime(System.currentTimeMillis());
        int i = scoreService.evaluateScore(s);
        if (i > 0) Result.success();
        return Result.response_400();
    }
}
